package com.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.entity.Menu;
import com.entity.User;
import com.service.menurole.MenuServiceImpl;
import com.utils.EmptyUtils;
import com.utils.ReturnResult;

@WebServlet("/MenuServlet")
public class MenuServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	public String index(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//先查询全部的权限
		int id = EmptyUtils.isEmpty(request.getParameter("id"))? 0:Integer.parseInt(request.getParameter("id"));
		request.setAttribute("userid",id);
		List<Menu> list2 = new MenuServiceImpl().allMenuByid(id);
		request.setAttribute("list2", list2);
		List<Menu> list = new MenuServiceImpl().allMenuByfp();
		request.setAttribute("menulist", list);
		//权限按钮menuBtn
		User user = (User) request.getSession().getAttribute("easybuyUserLogin");
		List<Menu> menuBtn = new MenuServiceImpl().menuBtn(user.getId(),3);
		request.setAttribute("menuBtn", menuBtn);
		return "backend/menu/menuList";
	}
	
	public ReturnResult menuByUserid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ReturnResult rr = new ReturnResult();
		int id = Integer.parseInt(request.getParameter("userid"));
		List<Menu> list = new MenuServiceImpl().allMenuByid(id);
		rr.returnYes(list);
		return rr;
	}
	
	public ReturnResult fpMenu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ReturnResult rr = new ReturnResult();
		String[] array = request.getParameterValues("menuids");
		int userid = Integer.parseInt(request.getParameter("userid"));
		if(EmptyUtils.isEmpty(array)) {
			int delRow = new MenuServiceImpl().delMenuByUserid(userid);
			if(delRow == 0) {
				rr.returnYes("成功取消所有权限");
			}
		}else {
			int row = new MenuServiceImpl().fpMenu(userid,array);
			if(row == 1) {
				rr.returnYes("分配成功");
			}
		}
		return rr;
	}
	
	/**
	 * 删除权限
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public int deleteMenu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int menuid = Integer.parseInt(request.getParameter("menuid"));
		int row = new MenuServiceImpl().deleteMenu(menuid);
		return row;
	}
	
	/**
	 * 查询单个用户的信息
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String seleMenuByMenuid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int menuid = Integer.parseInt(request.getParameter("menuid"));
		request.setAttribute("menuid", menuid);
		Menu menu = new MenuServiceImpl().menuBymenuid(menuid);
		request.setAttribute("menu", menu);
		return "backend/menu/toUpdateMenu";
	}
	
	/**
	 * 修改权限
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public ReturnResult toUpdateMenu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ReturnResult result = new ReturnResult();
		
		return result;
	}
	
	/**
	 * 新增权限
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public ReturnResult addMenu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ReturnResult result = new ReturnResult();
		
		return result;
	}
	

	@Override
	public Class<?> getServletClass() {
		return MenuServlet.class;
	}

}
